<?php
	
	include_once "../../php/inc/defines.php";
	include_once "../../php/auth.php";
	
	session_start();
	auth::secure(ADMIN);
	
	if(isset($_GET["id"])) {
		$id = $_GET["id"];
		$db = new PDO(SERVER.":host=".HOST.";dbname=".BASE, USER, PASS);
		
		//recuperer toutes les formations existantes
		$sqlQuery = "SELECT * from ".FORM_TABLE.";";
		$st = $db->prepare($sqlQuery);
		$st->execute();
		$formations = array();
		while($ligne = $st->fetch(PDO::FETCH_ASSOC))
			$formations[$ligne["id_form"]] = $ligne["nom_form"];
		
		//recuperer le directeur des etudes
		$id = $_GET["id"];
		$sqlQuery = "SELECT id_dde, login, password FROM ".DDE_TABLE." WHERE id_dde=:id;";
		$st = $db->prepare($sqlQuery);
		$st->bindValue(':id', $id, PDO::PARAM_STR);
		$st->execute();
		$dde = $st->fetch(PDO::FETCH_ASSOC);
		
		//recuperer les formations de ce directeur des etudes
		$id = $_GET["id"];
		$sqlQuery = "SELECT id_form FROM ".SUIT_TABLE." WHERE id_dde=:id;";
		$st = $db->prepare($sqlQuery);
		$st->bindValue(':id', $id, PDO::PARAM_STR);
		$st->execute();
		$formationsAssociees = array();
		while($ligne = $st->fetch(PDO::FETCH_ASSOC))
			array_push($formationsAssociees, $ligne["id_form"]);
		
		echo "<!DOCTYPE html>";
		echo "<html>";
		echo "<head>";
		echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">";
		echo "<title>Fiche du directeur des &eacute;tudes</title>";
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../../css/styles.css\">";
		echo "</head>";
		echo "<body class=\"fiche\">";
		echo "<div class=\"ficheEtu\">";
		echo "<div class=\"ficheTitre\">";
		echo "Fiche de ".$dde["login"];
		echo "</div>";
		echo "<form id=\"form\" action=\"../../php/doddeficheaction.php\" method=\"post\">";
		echo "<table class=\"ficheTable\" cellpadding=\"5\">";
		echo "<tr><td class=\"columnLabel\">Formation :</td><td class=\"columnLabel\">Association :</td></tr>";
		foreach($formations as $id_form=>$nom_form) {
			echo "<tr>";
			echo "<td><label for=\"".$id_form."\">".$nom_form."</label></td><td class=\"center\"><input type=\"checkbox\" id=\"".$id_form."\" name=\"".$id_form."\" value=\"".$id_form."\"";
			if(in_array($id_form, $formationsAssociees))
				echo " checked";
			echo "></td>";
			echo "</tr>";
		}
		echo "</table>";
		echo "<div class=\"ficheButtonsDiv\">";
		echo "<input type=\"hidden\" name=\"id\" value=\"".$id."\"><input class=\"formElement ficheButton\" type=\"submit\" value=\"Appliquer\">";
		echo "<input class=\"formElement ficheButton\" type=\"button\" value=\"Fermer\" onclick=\"window.close()\">";
		echo "</div>";
		echo "</form>";
		echo "</div>";
		echo "</body>";
		echo "</html>";
	}
	
?>
